﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class money : MonoBehaviour
{
    public bool isMoney = true;
    float speed = 10;
    bool isRun = false;

    bool isCitie = false;
    private void Start()
    {
        isCitie = GameController.instance.isCitie;
        if (isCitie)
            OnCitie(null);
        else
            OnNoCitie(null);
        MessageCenter.instance.AddEvtListener(MsgType.citie, OnCitie);
        MessageCenter.instance.AddEvtListener(MsgType.noCitie, OnNoCitie);
    }

    private void OnCitie(CustomMessage cm)
    {
        isCitie = true;
        GetComponent<BoxCollider>().size = new Vector3(18, 15, 2);
    }

    private void OnNoCitie(CustomMessage cm)
    {
        isCitie = false;
        isRun = false;
        GetComponent<BoxCollider>().size = new Vector3(2, 2, 2);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 8)
        {
            if (isCitie)
            {
                GetComponent<BoxCollider>().enabled = false;
                isRun = true;
            }
            else
            {
                if (isMoney)
                    GameController.instance.rocket.GetComponent<rocket>().MoneyPlus();
                else
                    GameController.instance.rocket.GetComponent<rocket>().PowerPlus();
                Destroy(this.gameObject);
            }
        }
    }

    private void Update()
    {
        if (isRun)
        {
            if (Vector3.Distance(GameController.instance.rocket.transform.position, transform.position) < 0.75f)
            {
                if (isMoney)
                    GameController.instance.rocket.GetComponent<rocket>().MoneyPlus();
                else
                    GameController.instance.rocket.GetComponent<rocket>().PowerPlus();
                Destroy(this.gameObject);
            }
            speed += 0.5f;
            transform.Translate((GameController.instance.rocket.transform.position - transform.position) * Time.deltaTime * speed, Space.World);
        }
    }

    private void OnDestroy()
    {
        MessageCenter.instance.RemoveEvtListener(MsgType.citie, OnCitie);
        MessageCenter.instance.RemoveEvtListener(MsgType.noCitie, OnNoCitie);
    }
}